//
//  XiaoXiReciveVC.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/30.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "XiaoXiReciveVC.h"
#import "SYRequestAPI+HomePage.h"
#import "NoticeMessageList.h"
#import "XGMessageModelOfMe.h"
#import "ChengJiYiChangVC.h"
#import "SYKaoQinYiChangViewController.h"
#import "CuoTiLvVC.h"
#import "SYKaoQinYiChangViewController.h"
#import "SYHouSanMingViewController.h"

@interface XiaoXiReciveVC ()
@property (nonatomic, strong) NSMutableArray *messageListArray;
/**
 成绩异常
 */
@property (nonatomic, strong) ChengJiYiChangVC *loginHomePageVC;

/**
 考勤异常
 */
@property (nonatomic, strong) SYKaoQinYiChangViewController *kqycVC;

/**
 错题率
 */
@property (nonatomic, strong) CuoTiLvVC *cuoTiLvView;

/**
 成绩后三名
 */
@property (nonatomic, strong) SYHouSanMingViewController *hsmVC;

@end

@implementation XiaoXiReciveVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)initSubView{
    _messageListArray = [NSMutableArray new];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableList];
    _tableList.hidden = YES;
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0).offset(0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [self.view addSubview:self.nullImageView];
//    _nullImageView.hidden = YES;
    [_nullImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.equalTo(@207);
        make.height.equalTo(@196);
        make.centerY.equalTo(self.view.mas_centerY);
    }];
    [self getMessageList];
    [_tableList headerRefreshBlock:^{
        [self getMessageList];
    }];
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 144, self.view.frame.size.width-200, APP_HEIGHT-84) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        [tableView registerClass:[XiaoXiRedDotCell class] forCellReuseIdentifier:@"XiaoXiRedDotCell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _messageListArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 20;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NoticeMessageList *model = _messageListArray[indexPath.row];
    NSDictionary *dic = dicWithJsonString(model.MsgContent);
    XGMessageModelOfMe *m = [XGMessageModelOfMe yy_modelWithJSON:dic];
    [self chuLiMessage:m];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XiaoXiRedDotCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XiaoXiRedDotCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[XiaoXiRedDotCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"XiaoXiRedDotCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NoticeMessageList *model = _messageListArray[indexPath.row];
    cell.dateLabel.text = model.CreateDate;
    NSDictionary *dic = dicWithJsonString(model.MsgContent);
    cell.titleLabel.text = [dic objectForKey:@"body"];
    cell.classLabel.text = model.MsgTypeName;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0,0, self.view.frame.size.width, 20);
    return view;
}
#pragma mark - 异常处理
- (void)chuLiMessage:(XGMessageModelOfMe *)model{
    if ([model.type isEqualToString:@"101"]) {
        //考勤异常
        
        _kqycVC = [[SYKaoQinYiChangViewController alloc] init];
        _kqycVC.view.frame = CGRectMake(0, 0, APP_WIDTH, APP_HEIGHT);
        _kqycVC.xgModel = model;
        [_kqycVC requestDataListArr];
        [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:_kqycVC.view];
    }else if ([model.type isEqualToString:@"102"]){
        //排名异常
        _loginHomePageVC = [[ChengJiYiChangVC alloc] init];
        _loginHomePageVC.view.frame = CGRectMake(0, 0, APP_WIDTH, APP_HEIGHT);
        _loginHomePageVC.model = model;
        [_loginHomePageVC getPaiMingChengJiYiChangUp];
        [_loginHomePageVC getPaiMingChengJiYiChangDown];
        [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:_loginHomePageVC.view];
    }else if ([model.type isEqualToString:@"103"]){
        //错题率
        _cuoTiLvView = [[CuoTiLvVC alloc]init];
        _cuoTiLvView.msgId = model.msgId;
        [_cuoTiLvView getRequest];
        [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:_cuoTiLvView.view];
        //        [XSInfoView showInfo:@"此功能正在开发中..." onView:[UIApplication sharedApplication].keyWindow.rootViewController];
    }else if ([model.type isEqualToString:@"104"]){
        //成绩后三名
        _hsmVC = [[SYHouSanMingViewController alloc] init];
        _hsmVC.xgModel = model;
//        [_hsmVC requestData];
        [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:_hsmVC.view];
    }
}
- (UIImageView *)nullImageView{
    if (!_nullImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"xiaoxi_null"];
        _nullImageView = imageView;
    }
    return _nullImageView;
}
#pragma mark - 得到消息列表
- (void)getMessageList{
    [self.messageListArray removeAllObjects];
    [SYRequestAPI sy_getNoticeNoticeReceiveServiceGetNoticeInfo:@{} isUnread:YES success:^(id  _Nonnull responseObject) {
        NSArray *array = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            NoticeMessageList *model = [NoticeMessageList yy_modelWithJSON:dic];
            [self->_messageListArray addObject:model];
        }
        self->_nullImageView.hidden = YES;
        self->_tableList.hidden = NO;
        [self->_tableList reloadData];
        [self->_tableList.mj_header endRefreshing];
    } fail:^(NSError *error) {
        [self->_tableList.mj_header endRefreshing];
    }];
}
#pragma 字符串转换成字典
NS_INLINE NSDictionary *dicWithJsonString(NSString *jsonString)
{
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    if(err) {
//        NSLog(@"json解析失败：%@",err);
        return nil;
    }
//    NSLog(@"得到消息列表 %@",dic);
    return dic;
}
@end
